<template>
  <el-container class="layout-container">
    <el-header class="header">
      <div class="header-left">
        <h1 class="system-title">初装家居室内空气质量监测系统</h1>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            {{ username }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          @select="handleSelect"
          router
        >
          <el-menu-item index="/home">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-menu-item index="/family">
            <el-icon><User /></el-icon>
            <span>家庭管理</span>
          </el-menu-item>
          
          <el-menu-item index="/room">
            <el-icon><HomeFilled /></el-icon>
            <span>房间管理</span>
          </el-menu-item>
          
          <el-menu-item index="/sensor">
            <el-icon><Monitor /></el-icon>
            <span>传感器管理</span>
          </el-menu-item>
          
          <el-menu-item index="/air-data">
            <el-icon><DataLine /></el-icon>
            <span>空气数据</span>
          </el-menu-item>
          
          <el-sub-menu index="/analysis">
            <template #title>
              <el-icon><TrendCharts /></el-icon>
              <span>空气分析</span>
            </template>
            <el-menu-item index="/analysis/bar">柱状图</el-menu-item>
            <el-menu-item index="/analysis/line">线性图</el-menu-item>
          </el-sub-menu>
          
          <el-menu-item index="/alarm">
            <el-icon><Warning /></el-icon>
            <span>报警记录</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主要内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'
import {
  House,
  User,
  HomeFilled,
  Monitor,
  DataLine,
  TrendCharts,
  Warning
} from '@element-plus/icons-vue'

const router = useRouter()
const username = ref('')
const activeMenu = ref('/home')

// 获取用户信息
const getUserInfo = () => {
  const userInfo = localStorage.getItem('userInfo')
  if (userInfo) {
    const { username: name } = JSON.parse(userInfo)
    username.value = name
  } else {
    // 如果没有用户信息，重定向到登录页
    router.push('/login')
  }
}

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    handleLogout()
  }
}

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 清除本地存储的用户信息和token
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
      
      // 提示退出成功
      ElMessage.success('退出成功')
      
      // 跳转到登录页
      router.push('/login')
    })
    .catch(() => {
      // 取消退出
      ElMessage.info('已取消退出')
    })
}

// 组件挂载时获取用户信息
onMounted(() => {
  getUserInfo()
})

const handleSelect = (index) => {
  activeMenu.value = index
}
</script>

<style scoped>
.layout-container {
  margin-left: -230px;
  width: 180vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  flex: 1;
}

.header-right {
  margin-left: 20px;
}

.system-title {
  font-family: "楷体";
  font-size: 30px;
  margin: 0;
  text-align: left;
  color: #303133;
}

.el-menu-vertical {
  height: 100%;
  border-right: 1px solid #dcdfe6;
}

.el-aside {
  background-color: #fff;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  display: flex;
  align-items: center;
}

.el-dropdown-link:hover {
  color: #66b1ff;
}

.footer {
  display: none;
}
</style> 